import { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import {
  isLoginOrRegister,
  isNoNeedUserInfo,
  LOGIN_PATHNAME,
  MANAGE_LIST_PATHNAME,
} from "../router";
import useGetUserInfo from "./useGetUserInfo";

function useNavPage() {
  const nav = useNavigate();
  const userInfo = useGetUserInfo();
  const { pathname } = useLocation();

  useEffect(() => {
    if (userInfo.userName) {
      if (isLoginOrRegister(pathname)) return nav(MANAGE_LIST_PATHNAME);
      return;
    }
    if (!isNoNeedUserInfo(pathname)) return nav(LOGIN_PATHNAME);
  }, [userInfo, pathname]);
}

export default useNavPage;
